{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-20T14:12:43.476933Z",
     "start_time": "2025-07-20T14:12:43.464933Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 导入必要的库\n",
    "import torch\n",
    "from modelscope import AutoTokenizer, AutoModel\n",
    "from peft import PeftModel, PeftConfig"
   ],
   "id": "89c50f0e26bd1b9d",
   "outputs": [],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-20T14:12:46.738143Z",
     "start_time": "2025-07-20T14:12:46.723668Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 定义模型和预训练模型的路径\n",
    "model_dir = \"C:\\\\Users\\\\16014\\\\.cache\\\\modelscope\\\\hub\\\\models\\\\ZhipuAI\\\\chatglm3-6b\"\n",
    "peft_model_id = \"./lora_saver_v2/lora_query_key_value_20250720\""
   ],
   "id": "bfb409b797d070ba",
   "outputs": [],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-20T14:13:05.118516Z",
     "start_time": "2025-07-20T14:12:49.502347Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 在不计算梯度的情况下进行模型加载和预处理\n",
    "with torch.no_grad():\n",
    "    tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True)\n",
    "    model = AutoModel.from_pretrained(model_dir, trust_remote_code=True).half().cuda()\n",
    "    "
   ],
   "id": "473bfebd618bcfc4",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Loading checkpoint shards:   0%|          | 0/7 [00:00<?, ?it/s]"
      ],
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "9a5ec441a64b47259c23da096caece23"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-20T14:13:07.568403Z",
     "start_time": "2025-07-20T14:13:07.469856Z"
    }
   },
   "cell_type": "code",
   "source": [
    "model = PeftModel.from_pretrained(model, peft_model_id)\n",
    "model.eval()\n",
    "\n",
    "history = []\n",
    "role = \"user\""
   ],
   "id": "fe1d9717c1a311ea",
   "outputs": [],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-20T14:13:34.233712Z",
     "start_time": "2025-07-20T14:13:08.845717Z"
    }
   },
   "cell_type": "code",
   "source": [
    "while True:\n",
    "    # 通过键盘接收用户输入\n",
    "    query = input(\"请输入您的问题：\")\n",
    "\n",
    "    # 判断用户是否想退出对话\n",
    "    if query.lower() == \"~\":\n",
    "        break\n",
    "\n",
    "        # 使用分词器构建聊天输入\n",
    "    inputs = tokenizer.build_chat_input(query, history=history, role=role)\n",
    "    inputs = inputs.to('cuda')\n",
    "\n",
    "    # 定义结束标记和生成参数\n",
    "    eos_token_id = [tokenizer.eos_token_id, tokenizer.get_command(\"<|user|>\"), tokenizer.get_command(\"<|observation|>\")]\n",
    "    gen_kwargs = {\"max_length\": 1200, \"num_beams\": 1, \"do_sample\": True, \"top_p\": 0.8, \"temperature\": 0.8}\n",
    "\n",
    "    # 生成输出\n",
    "    outputs = model.generate(**inputs, **gen_kwargs, eos_token_id=eos_token_id)\n",
    "    outputs = outputs.tolist()[0][len(inputs[\"input_ids\"][0]):-1]\n",
    "\n",
    "    # 使用分词器解码输出\n",
    "    response = tokenizer.decode(outputs)\n",
    "\n",
    "    # 处理响应，包括去除一些特殊标记和更新对话历史\n",
    "    response, history = model.process_response(response, history)\n",
    "\n",
    "    # 打印响应\n",
    "    print(\"模型响应：\", response)"
   ],
   "id": "321bd8cf3449cac4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型响应： 我叫茂茂，现在是你的购物助理机器人。\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "d129ef4cf6fb99ac"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
